Certified Java Programmer Mock Exam


Question 1


1.  class Red {
2.    // Comment1
3.    // // Comment2
4.    /* Comment3 */
5.    /* Comment4
6.    */
7.    /** Comment5 */
8.    // /* Comment6 */
9.    /* // Comment7 */
10.   /* /* Comment8 */
11.   /** /* // Comment9 // /* */
12.   /* Comment10 */*/
13.   /* Comment11 /* Comment12 */ */
14. }

What is the result of attempting to compile the program?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. Compiler error at line 6.
g. Compiler error at line 7.
h. Compiler error at line 8.
i. Compiler error at line 9.
j. Compiler error at line 10.
k. Compiler error at line 11.
l. Compiler error at line 12.
m. Compiler error at line 13.
n. None of the Above

Question 2


class A {
  A() throws Exception {}  // 1
}
class B extends A {
  B() throws Exception {}  // 2
}
class C extends A {}       // 3

Which of the following statements are true?

a. Compiler error at 1.
b. Compiler error at 2.
c. Compiler error at 3.
d. None of the Above

Question 3


class G {
  public static void main(String[] args) {
    int[][] a1 = {{1;2};{3;4;5};{6;7;8;9}};
    System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 4


class I {
  public static void main(String[] args) {
    int[][] a1 = [[1,2],[3,4,5],[6,7,8,9]];
    int[][] a2 = [[1,2],[3,4,5],[6,7,8,9]];
    int[][] a3 = [[1,2],[3,4,5],[6,7,8,9]];
    System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 5



Which of the following modifiers can be applied to a class that is not a nested class?

a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 6


class B {
  public static void main(String[] args) {
    int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}};
    int []a2[] = {{1,2},{3,4,5},{6,7,8,9}};
    int a3[][] = {{1,2},{3,4,5},{6,7,8,9}};
    System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compiler Error.
g. Runtime Error.
h. None of the Above.

Question 7


class A {
  public static void main(String[] args) {
    int[] a1 = new int[];              // 1
    int a2[] = new int[5];             // 2
    int[] a3 = new int[]{1,2};         // 3
    int []a4 = {1,2};                  // 4
    int[] a5 = new int[5]{1,2,3,4,5};  // 5
  }
}

Compile-time errors occur at which lines?

a. 1.
b. 2.
c. 3.
d. 4.
e. 5.
f. None of the Above.

Question 8



Which of the following is used to prevent the serialization of a field?

a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 9


public class A {
  int i1;
  void m1() {}
}

Which of the following statements are true?

a. class A extends Object.
b. Field i1 is implicitly public because class A is public.
c. Method m1 is implicitly public because class A is public.
d. The compiler will insert a default constructor implicitly.
e. The implicit constructor declaration has no throws clause.
f. The implicitly declared constructor has package access.
g. The implicitly declared constructor accepts one parameter for each field in class A.
h. The implicitly declared constructor invokes the no-parameter constructor of the superclass.
i. None of the Above

Question 10


class A {
  transient float e = 1;         // 1
  volatile float f  = 1;         // 2
  abstract float j = 1;          // 3
  final float g = 1;             // 4
  private final float k = 1;     // 5
  private transient float l = 1; // 6
  volatile final float m = 1;    // 7
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. None of the above.

Question 11


class A {
  public static void main(String[] args) {
    int[5] a1;  // 1
    int []a2;   // 2
    int[   ]a3; // 3
    int[] a4[]; // 4
  }
}

Compile-time errors occur at which lines?

a. 1.
b. 2.
c. 3.
d. 4.
e. None of the Above.

Question 12



Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class?

a. Do not declare any constructors.
b. Do not use a return statement in the constructor.
c. Declare all constructors with a void return type.
d. Declare all constructors using the private access modifier.
e. None of the above.

Question 13


class H {
  public static void main(String[] args) {
    int[][] a1 = ((1,2),(3,4,5),(6,7,8,9));
    System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 14



Which of the following statements are true?

a. The compiler will create a default constructor if no other constructor is declared.
b. The default constructor takes no parameters.
c. The default constructor invokes the superclass constructor with no arguments.
d. The default constructor declares Exception in the throws clause.
e. The default constructor is always given the private access modifier.
f. The default constructor is always given the public modifier.
g. The default constructor is always given default package access.
h. None of the above.

Question 15


class C {
  public static void main(String[] args) {
    int[] a1[] = {new int[]{1,2},new int[]{3,4,5}};
    int []a2[] = new int[][]{{1,2},{3,4,5}};
    int a3[][] = {{1,2},new int[]{3,4,5}};
    System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,2,4
d. Prints: 2,3,4
e. Prints: 2,3,5
f. Prints: 2,4,5
g. Compiler Error.
h. Runtime Error.
i. None of the Above.

Question 16


class A {
  public static void main(String[] args) {
    int[][] a = {{1,2},{3,4,5},{6,7,8,9},{}};
    System.out.print(a.length);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0
b. Prints: 3
c. Prints: 4
d. Prints: 9
e. Prints: 10
f. Prints: 11
g. Compiler Error.
h. Runtime Error.
i. None of the Above.

Question 17


class A {}
class B {
  public static void main(String[] arg) {
    A[]     a1 = new A[1];         // 1
    A[][]   a2 = new A[2][1];      // 2
    A[][][] a3 = new A[3][3][3];   // 3
    System.out.print(a3[2][2][2]); // 4
    a1[0] = new A();               // 5
    a2[0] = a2[1] = a1;            // 6
    a3[0] = a3[1] = a3[2] = a2;    // 7
    System.out.print(a3[2][2][2]); // 8
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: null
b. Prints: nullnull
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Compile-time error at 8.
k. Run-time Exception
l. None of the Above

Question 18


class A {
  public static void main (String[] args) {
    int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        System.out.print(a1[j][i]);
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 123456789
b. Prints: 147258369
c. Prints: 321654987
d. Prints: 369258147
e. Run time Exception
f. Compiler Error
g. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1